package com.gxa.jd.controller.front;

import com.gxa.entity.Result;
import com.gxa.exception.CustomerException;
import com.gxa.jd.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;


@RequestMapping("/front/cart")
@Controller
@ResponseBody
public class CartController {

    @Autowired
    private CartService cartService;


    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result addGoodsToCart(Long goodsId, Integer num) throws CustomerException {

        //使用dubbo隐式传参后此处第一个参数可以省略
        cartService.addGoodsToCart(goodsId,num);
        return new Result();
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Result updateCartItemNum(Long goodsId,Integer num){
        cartService.updateCartItemNum(goodsId,num);
        return new Result();
    }

    @RequestMapping(value = "/removeCartItem",method = RequestMethod.POST)
    public Result removeCartItem(Long goodsId){
        cartService.removeCartItem(goodsId);
        return new Result();
    }

    @RequestMapping(value = "/clearCart",method = RequestMethod.POST)
    public Result clearCart(){
        cartService.clearCart();
        return new Result();
    }

    @RequestMapping(value = "/queryUserCart",method = RequestMethod.POST)
    public Result queryUserCart(){
        Map result = cartService.queryUserCart();
        return new Result(result);
    }

}
